using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using RideMe.Core.Properties;

namespace RideMe.Core
{
    public class ProjectProcess : ProcessBase   
    {
        private System.Threading.Timer _timer;
        private bool _complete;

        private string _projectPath;

        /// <summary>
        /// Gets or sets the project path.
        /// </summary>
        /// <value>The project path.</value>
        public string ProjectPath
        {
            get { return _projectPath; }
            set { _projectPath = value; }
        }

        /// <summary>
        /// Creates the project.
        /// </summary>
        /// <param name="path">The path.</param>
        public void CreateProject(string path)
        {
            _projectPath = path;

            CreateProject();
        }

        /// <summary>
        /// Creates the project.
        /// </summary>
        public void CreateProject()
        {
            if ((_projectPath == null) || (_projectPath.Trim().Length == 0))
            {
                throw new ArgumentException(Resources.ProjectPathNotSetError);
            }

            ExecuteCreateProjectCommand();

            _timer = new System.Threading.Timer(TimerElapsedCallback, null, 60000, 1000);

        }

        /// <summary>
        /// Executes the create project command.
        /// </summary>
        private void ExecuteCreateProjectCommand()
        {

            // If the path has spaces in it, and isn't surrounded by quotes,
            // the rails command will create the project in a directory leading
            // up to the first space. Just put quotes around it.
            RunCommand(Validator.QuoteString(_projectPath), false);
        }

        /// <summary>
        /// Deletes the project.
        /// </summary>
        /// <param name="path">The path.</param>
        public static void DeleteProject(string path)
        {
            if (!Validator.ValidDirectory(path))
            {
                throw new ArgumentException(Resources.DirectoryDoesNotExist);
            }

            Directory.Delete(path, true);
        }

        /// <summary>
        /// Runs a project specific command.
        /// </summary>
        /// <param name="arguments"></param>
        public override void RunCommand(string arguments, bool redirectStandardError)
        {
            ProcessExecutor projectExecutor = this.CreateCommand();
            
            string railsCommand = Options.Instance.RubyPath + Resources.RailsCommandPath;

            if (Validator.ValidFile(railsCommand))
            {
                projectExecutor.ExecuteCommand(railsCommand, arguments, redirectStandardError);
            }
            else
            {
                throw new FileNotFoundException();
            }
        }

        protected override void OnCompleted(EventArgs e)
        {
            base.OnCompleted(e);

            _complete = true;
        }

        /// <summary>
        /// Callback method that fires when the timer due time is up.
        /// </summary>
        /// <param name="state"></param>
        private void TimerElapsedCallback(object state)
        {
            if (!_complete)
            {
                OnCompleted(EventArgs.Empty);
            }
            _timer.Dispose();
        }

    }
}
